#include "include.h"


void timer_callback( void )
{
  rk.timer.get_sensor_value_timer++;
  rk.timer.system_work_timer++;
  rk.timer.device_login_timer++;

  if( rk.CONFIG.HUART2 )
  {
    rk.timer.upload_to_server_heart_timer++;
  }
  if( rk.CONFIG.ENCODER )
  {
    if( rk.flag.gate_in_motion == true )
    {
      rk.timer.gata_move_stop_timer++;
    }
  }

  if( rk.CONFIG.WATER_LEVEL )
  {
    rk.timer.water_level_upstream_stop_work_timer++;
    rk.timer.water_level_downstream_stop_work_timer++;
  }
  if( rk.CONFIG.FLOWMETER )
  {
    rk.timer.flowmeter_stop_work_timer++;
  }

  if( rk.CONFIG.EC20 )
  {
    rk.timer.proc_get_single_timer++;
    rk.timer.EC20_init_timer++;
    rk.timer.EC20_connect_timer++;
    rk.timer.EC20_signal_timer++;
    rk.timer.EC20_get_signal_timer++;
    rk.timer.upload_to_server_timer++;
    rk.timer.EC20_stop_work_timer++;
    rk.timer.function_data_upload_timer++;
    rk.timer.THA_data_upload_timer++;
    rk.timer.scan_alarm_timer++;
    rk.timer.scan_malfunction_timer++;
    rk.timer.system_sync_clock_timer++;
    rk.timer.wait_confirm_timeout_timer++;

    if( !rk.flag.EC20_trigger )
    {
      rk.timer.EC20_trigger_timer++;
    }
    if( rk.flag.EC20_shutdown )
    {
      rk.timer.EC20_shutdown_timer++;
    }
  }
  if( rk.CONFIG.LED )
  {
    rk.timer.led_work_timer++;
  }
  if( rk.CONFIG.SCREEN )
  {
    rk.timer.screen_refresh_timer++;
    rk.timer.screen_stop_push_timer++;
  }
  rk.timer.RS485_transmit_timer++;
  rk.timer.gate_control_timer++;

  if( rk.gate.detection_move_state != Gate_State_Is_Stop )
  {
    rk.timer.encoder_recv_stop_timer++;
  }
  if( rk.gate.opening_degree != Gate_Opening_Degree_0 )
  {
    if( rk.flowmeter.water_level == rk.hyper_parameter.gate_front_water_stage_zero )
    {
      rk.timer.sensor_water_level_timer++;
    }
    else
    {
      rk.timer.sensor_water_level_timer = 0;
    }

    if( rk.flowmeter.instant_water_discharge == 0 )
    {
      rk.timer.sensor_flowmeter_timer++;
    }
    else
    {
      rk.timer.sensor_flowmeter_timer = 0;
    }
  }
  else
  {
    rk.timer.sensor_water_level_timer = 0;
    rk.timer.sensor_flowmeter_timer = 0;
  }
}


void RTC_Settime( unsigned char *timedata )
{
  RTC_TimeTypeDef wTime 	= {0};
  RTC_DateTypeDef wDate 	= {0};
  wDate.Year                    = timedata[6] >> 2;
  wDate.Month                   = (timedata[6] & 0x03) << 2 | (timedata[7] & 0xC0) >> 6;
  wDate.Date                    = (timedata[7] & 0x3F) >> 1;
  wTime.Hours                   = (timedata[7] & 0x01) << 4 | timedata[8] >> 4;
  wTime.Minutes                 = (timedata[8] & 0x0F) << 2 | timedata[9] >> 6;
  wTime.Seconds                 = (timedata[9] & 0x3F);
  HAL_RTC_SetTime( &hrtc, &wTime, RTC_FORMAT_BIN );
  HAL_RTC_SetDate( &hrtc, &wDate, RTC_FORMAT_BIN );
}

void rtc_settime( unsigned int timedata )
{
  RTC_TimeTypeDef wTime = {0};
  RTC_DateTypeDef wDate = {0};

  wTime.Seconds 	= timedata & 0x3F;

  timedata >>= 6;
  wTime.Minutes		= timedata & 0x3F;

  timedata >>= 6;
  wTime.Hours	        = timedata & 0x1F;

  timedata >>= 5;
  wDate.Date	        = timedata & 0x1F;

  timedata >>= 5;
  wDate.Month	        = timedata & 0x0F;

  timedata >>= 4;
  wDate.Year	        = timedata & 0x1F;

  HAL_RTC_SetTime( &hrtc, &wTime, RTC_FORMAT_BIN );
  HAL_RTC_SetDate( &hrtc, &wDate, RTC_FORMAT_BIN );
}

void rtc_gettime( unsigned int *timedata )
{
  RTC_TimeTypeDef rTime 	= {0};
  RTC_DateTypeDef rDate 	= {0};
  unsigned int    tmptime =  0;

  // Get RTC
  HAL_RTC_GetTime(&hrtc, &rTime, RTC_FORMAT_BIN);
  HAL_RTC_GetDate(&hrtc, &rDate, RTC_FORMAT_BIN);


  tmptime 	|= rDate.Year & 0x3F;
  tmptime 	 = tmptime << 4;

  tmptime 	|= rDate.Month & 0x0F;
  tmptime 	 = tmptime << 5;

  tmptime 	|= rDate.Date & 0x1F;
  tmptime 	 = tmptime << 5;

  tmptime 	|= rTime.Hours & 0x1F;
  tmptime 	 = tmptime << 6;

  tmptime 	|= rTime.Minutes & 0x3F;
  tmptime 	 = tmptime << 6;

  tmptime 	|= rTime.Seconds & 0x3F;

  *timedata	= tmptime;
}

void get_datetime_from_dt( unsigned int timedata, unsigned char *rdt )
{
  rdt[0] 	                = timedata & 0x3F;

  timedata >>= 6;
  rdt[1]		        = timedata & 0x3F;

  timedata >>= 6;
  rdt[2]			= timedata & 0x1F;

  timedata >>= 5;
  rdt[3]			= timedata & 0x1F;

  timedata >>= 5;
  rdt[4]			= timedata & 0x0F;

  timedata >>= 4;
  rdt[5]			= timedata & 0x1F;
}

void set_device_clock( uint8_t *data  )
{
  RTC_TimeTypeDef wTime = {0};
  RTC_DateTypeDef wDate = {0};

  rk.upload.confirm_type = AFN_Set_Device_Clock;

  wDate.Year	= data[0];
  wDate.Month	= data[1];
  wDate.Date	= data[2];
  wTime.Hours	= data[3];
  wTime.Minutes	= data[4];
  wTime.Seconds = data[5];

  HAL_RTC_SetTime( &hrtc, &wTime, RTC_FORMAT_BIN );
  HAL_RTC_SetDate( &hrtc, &wDate, RTC_FORMAT_BIN );
  rk.flag.system_sync_clock_success = true;
}
void check_clock_sync( uint8_t *data )
{
//  uint8_t rdata[6];
//  RTC_TimeTypeDef rTime 	= {0};
//  RTC_DateTypeDef rDate 	= {0};
//
//  HAL_RTC_GetTime(&hrtc, &rTime, RTC_FORMAT_BIN);
//  HAL_RTC_GetDate(&hrtc, &rDate, RTC_FORMAT_BIN);
//  rDate.Year	= data[0];
//  rDate.Month	= data[1];
//  rDate.Date	= data[2];
//  rTime.Hours	= data[3];
//  rTime.Minutes	= data[4];
//  rTime.Seconds = data[5];
}
